package com.Tread;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingQueueing {
    private static final int FILE_QUEUE_SIZE = 10;

    private static final int SEARCH_THREADS = 100;

    private static final File DUMMY = new File("");

    private static BlockingQueue<File> queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE);

    private static String toFile = "D:\\Atom文件";

    public static void main(String[] args) {

        try (Scanner in = new Scanner(System.in)) {
            System.out.println("请输入你要搜索的目录");
            String path = in.nextLine();
            String keyWord = in.nextLine();

            Runnable enumerator = () -> {
                try {
                    enumerate(new File(path));
                    queue.put(DUMMY);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };


            new Thread(enumerator).start();

            /**
             * 启动大量搜索线程，每个线程搜索一个文件,
             */
            for (int i = 0; i < SEARCH_THREADS; i++) {
                Runnable searcher = () -> {
                    try {
                        boolean flag = true;
                        while (flag) {
                            File file = queue.take();
                            if (file.equals(DUMMY)) {
                                flag = false;
                                queue.put(file);
                            } else
                                searchKeyWord(file, keyWord);
                        }
                    } catch (InterruptedException | FileNotFoundException e) {
                        e.printStackTrace();
                    }
                };
                new Thread(searcher).start();
            }

        }
    }

    /**
     * 将该目录下所有文件作为生产者放进阻塞队列
     *
     * @param directory
     */
    public static void enumerate(File directory) throws InterruptedException {
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory())
                enumerate(file);
            else
                queue.put(file);
        }
    }

    /**
     * 搜索文件包含关键字
     *
     * @param file
     * @param keyWord
     * @throws FileNotFoundException
     */
    public static void searchKeyWord(File file, String keyWord) throws FileNotFoundException {
        try (Scanner in = new Scanner(file, "UTF-8")) {
            int lineNumber = 0;
            while (in.hasNextLine()) {
                lineNumber++;
                String line = in.nextLine();
                if (line.contains(keyWord)){
                    //该操作具有原子性，线程安全
                    Files.copy(file.toPath(), Paths.get(toFile + "\\"+ file.getName()));
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}

